$(document).ready(function() {
	// Define callback functions for channel
	Invitesq.onOpened = function() {
		console.log("Channel is opened");
	};
	
	Invitesq.onMessage = function(message){
		alert("Channel received message: " + message.data);
	};

	Invitesq.onError = function(){
		console.log("Connection error");
	};

	Invitesq.onClose = function(){
		console.log("Channel is closed");
	};

	socket = Invitesq.channel.open();
	socket.onopen = Invitesq.onOpened;
	socket.onmessage = Invitesq.onMessage;
	socket.onerror = Invitesq.onError;
	socket.onclose = Invitesq.onClose;
	
	// Declare classes
	Invitesq.ChatModel = Backbone.Model.extend({});
	
	Invitesq.ChatView = Backbone.View.extend({
		el: '#chat',
		
		events: {
			'click #send-chat-btn': 'sendChat'
		},
		
		sendChat: function() {
			chat = new Invitesq.ChatModel({
				message: this.$('#chat-input').val(),
				friend: this.$('#friend-id').val()
			});
			
			$.post('/chat/', chat.toJSON(), function(data){
				console.log(data);
			});
		}
	});
	
	
	// Initialize classes so that they manage the events and interaction
	Invitesq.chatView = VM.createView('chatView', function(){
		return new Invitesq.ChatView();
	});
	
});
